//
//  GTListLoader.m
//  WKWebViewStudy
//
//  Created by Jason Len on 2019/9/17.
//  Copyright © 2019年 Jason. All rights reserved.
//

#import "GTListLoader.h"
#import <AFNetworking.h>
#import "GTListItem.h"

@implementation GTListLoader



-(void)loadListDataWithFinishBlock:(GTListLoaderFinishBlock)finishBlock{
    NSString *urlString= @"http://v.juhe.cn/toutiao/index?type=top&key=97ad001bfcc2082e2eeaf798bad3d54e";
    NSURL *url = [NSURL URLWithString:urlString];
//    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    NSURLSession * session = [NSURLSession sharedSession];
    
    
//    __weak typeof(self) weakSelf = self;
    NSURLSessionDataTask *dataTask=[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        __strong typeof(weakSelf) strongSelf= weakSelf;
        NSError *jsonError;
        id jsonObj = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
        
#warning 类型检查
        __unused NSArray *dataArray = [((NSDictionary *) [((NSDictionary *)jsonObj) objectForKey:@"result"])objectForKey:@"data"];
        NSMutableArray *listItemArray = @[].mutableCopy;
        for(NSDictionary *info in dataArray){
            GTListItem *listItem = [[GTListItem alloc]init];
            [listItem configWithDictionary:info];
            [listItemArray addObject:listItem];
        }
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if(finishBlock){
                finishBlock(error==nil,listItemArray);
            }
        });
        
    }];	
    [self _archiveList];
    [dataTask resume];
    
}

//简单存储 key-value
-(void)_archiveList{
    
    //字符串
    [[NSUserDefaults standardUserDefaults]setObject:@"jason" forKey:@"name"];
    NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"name"];

    //字符串
    NSNumber *age = [NSNumber numberWithInt:33];
    NSInteger ageInteger = [age integerValue];

    
    //数组
    NSArray *a1 = @[@"a",@"b",@"c"];
    [[NSUserDefaults standardUserDefaults]setObject:a1 forKey:@"a1"];
    NSArray *a1Res = [[NSUserDefaults standardUserDefaults]arrayForKey:@"a1"];
    
    NSArray *a2 = @[@1,@2,@56,@99];
    [[NSUserDefaults standardUserDefaults]setObject:a2 forKey:@"a2"];
    NSArray *a2Res = [[NSUserDefaults standardUserDefaults]arrayForKey:@"a2"];
    
    //字典
    NSDictionary *d = @{@"name":@"jason",@"age":@33,@"address":@"成都市"};
    NSDictionary *d1 = [NSDictionary dictionaryWithObjectsAndKeys:@"v1",@"k1",@"v2",@"k2", nil];
    [[NSUserDefaults standardUserDefaults]setObject:d forKey:@"dic"];
     [[NSUserDefaults standardUserDefaults]setObject:d1 forKey:@"dic1"];
    NSDictionary *dRes = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"dic"];
    NSDictionary *d1Res = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"dic1"];
    NSLog(@"");
}

//使用AFNetworking 实现请求
-(void)loadListData1{
    NSString *urlString = @"http://v.juhe.cn/toutiao/index?type=top&key=97ad001bfcc2082e2eeaf798bad3d54e";
    NSURL *url=[NSURL URLWithString:urlString];
    [[AFHTTPSessionManager manager]GET:urlString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

@end
